package myapps.ttclient.activity.ppt;

import myapps.ttclient.Helper;
import myapps.ttclient.R;
import myapps.ttclient.adapter.PPTPickingClipArtAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class PPTPickingClipArtActivity extends Activity 
		implements OnClickListener, OnItemClickListener {

	GridView gridView;
	PPTPickingClipArtAdapter clipartAdapter;

	Button btnApply;
	Button btnCancel;

	Integer[] mClipArtIDs;

	int mCurClipArtID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Helper.updateFullscreenStatus(this, true);
		
		//Helper.keepScreenOn(this);
		
		this.setContentView(R.layout.picking_pointer);
		
		Intent myCallerIntent = getIntent();
		Bundle myBundle = myCallerIntent.getExtras();
		mCurClipArtID = myBundle.getInt("ClipArtId");

		mClipArtIDs = new Integer[] {
			R.drawable.ca00,
			R.drawable.ca01,
			R.drawable.ca02,
			R.drawable.ca03,
			R.drawable.ca04,
			R.drawable.ca05,
			R.drawable.ca06,
			R.drawable.ca07,
			R.drawable.ca08,
			R.drawable.ca09,
			R.drawable.ca10,
			R.drawable.ca11,
			R.drawable.ca12,
			R.drawable.ca13,
			R.drawable.ca14
		};

		gridView = (GridView) findViewById(R.id.gridview);

		clipartAdapter = new PPTPickingClipArtAdapter(this, mClipArtIDs);
		clipartAdapter.setmClipArtSelectedID(mCurClipArtID);

		gridView.setAdapter(clipartAdapter);

		gridView.setOnItemClickListener(this);

		btnApply = (Button) findViewById(R.id.btnApply);
		btnApply.setOnClickListener(this);

		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v == btnApply){
			Intent resultIntent = new Intent();

			Bundle myBundle = new Bundle();
			myBundle.putInt ("ClipArtId", mCurClipArtID);

			resultIntent.putExtras(myBundle);

			setResult(Activity.RESULT_OK, resultIntent);
			finish();
		} else if (v == btnCancel) {
			setResult(Activity.RESULT_CANCELED, null);
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		mCurClipArtID = position;
		clipartAdapter.setmClipArtSelectedID(position);
		clipartAdapter.notifyDataSetChanged();
	}
}
